#!/bin/bash
if [ "$3" = "inject" ] ; then
    runtime/deploy/bin/nutch inject urls
fi
length=0
if [ "$1" = "-depth" ] ; then
    length=$2
fi
for ((i = 0; i < $length; i++))
do
  echo "***********************************"
  echo " *                               *"
  echo "  *   Current Loop in $i times   *"
  echo " *                               *"
  echo "***********************************"
  ssh ubuntu@slave1 "cd && sh ./cleanMemCache.sh"
  ssh ubuntu@slave2 "cd && sh ./cleanMemCache.sh"
  sudo sh -c "sync;echo 3 > /proc/sys/vm/drop_caches"
  runtime/deploy/bin/nutch generate -topN 1000000
  sudo sh -c "sync;echo 3 > /proc/sys/vm/drop_caches"
  ssh ubuntu@slave1 "cd && sh ./cleanMemCache.sh"
  ssh ubuntu@slave2 "cd && sh ./cleanMemCache.sh"
  runtime/deploy/bin/nutch fetch -all -threads 30
  ssh ubuntu@slave1 "cd && sh ./cleanMemCache.sh"
  ssh ubuntu@slave2 "cd && sh ./cleanMemCache.sh"
  sudo sh -c "sync;echo 3 > /proc/sys/vm/drop_caches"
  runtime/deploy/bin/nutch parse -all
  ssh ubuntu@slave1 "cd && sh ./cleanMemCache.sh"
  ssh ubuntu@slave2 "cd && sh ./cleanMemCache.sh"
  sudo sh -c "sync;echo 3 > /proc/sys/vm/drop_caches"
  runtime/deploy/bin/nutch updatedb
  ssh ubuntu@slave1 "cd && sh ./cleanMemCache.sh"
  ssh ubuntu@slave2 "cd && sh ./cleanMemCache.sh"
  sudo sh -c "sync;echo 3 > /proc/sys/vm/drop_caches"
  runtime/deploy/bin/nutch elasticindex nutches -all
done;

#bin/nutch generate -topN 9223372036854775808
#bin/nutch fetch -all -threads 100
#bin/nutch parse -all
#bin/nutch updatedb
